#include "fileeditfactory.h"

#include <QDebug>
#include <QDir>
#include <QFileDialog>
#include <QRegExpValidator>

fileEditFactory::fileEditFactory(QObject *parent): QtAbstractEditorFactory<QtStringPropertyManager>(parent)
{

}


void fileEditFactory::connectPropertyManager(QtStringPropertyManager *manager)
{
    connect(manager,&QtStringPropertyManager::valueChanged,
         this,&fileEditFactory::slotPropertyChanged);

    connect(manager,&QtStringPropertyManager::regExpChanged,
         this,&fileEditFactory::slotRegExpChanged);

}

QWidget *fileEditFactory::createEditor(QtStringPropertyManager *manager, QtProperty *property, QWidget *parent)
{
    this->proper=property;
    editor = new LineEdit(parent);
    QRegExp regExp = manager->regExp(property);
    if (regExp.isValid()) {
        QValidator *validator = new QRegExpValidator(regExp, editor);
        editor->setValidator(validator);
    }
    editor->setText(manager->value(property));

    connect(editor, &LineEdit::textEdited,
            this,&fileEditFactory::slotSetValue);
    connect(editor,&LineEdit::enter,
            this,&fileEditFactory::slotLintEditClick);

    return editor;
}

void fileEditFactory::disconnectPropertyManager(QtStringPropertyManager *manager)
{
    disconnect(manager,&QtStringPropertyManager::valueChanged,
         this,&fileEditFactory::slotPropertyChanged);

    disconnect(manager,&QtStringPropertyManager::regExpChanged,
         this,&fileEditFactory::slotRegExpChanged);
}

void fileEditFactory::slotPropertyChanged(QtProperty *property, const QString &value)
{

}

void fileEditFactory::slotRegExpChanged(QtProperty *property, const QRegExp &regExp)
{

}

void fileEditFactory::slotSetValue(const QString &value)
{
    QtStringPropertyManager *manager = propertyManager(proper);
    manager->setValue(proper,value);

}

void fileEditFactory::slotLintEditClick()
{
    QString fileName="";
    QFileDialog *fileDialog = new QFileDialog();
    //定义文件对话框标题
    fileDialog->setWindowTitle(tr("打开文件"));
    //设置默认文件路径
    fileDialog->setDirectory("");
    //设置文件过滤器
    fileDialog->setNameFilter(tr("text(*.*)"));
    //设置可以选择多个文件,默认为只能选择一个文件QFileDialog::ExistingFiles
    fileDialog->setFileMode(QFileDialog::ExistingFile);
    //设置视图模式
    fileDialog->setViewMode(QFileDialog::Detail);

    if (fileDialog->exec())
    {
        fileName=fileDialog->selectedFiles().at(0);
        QDir dir(QDir::currentPath());
        fileName=dir.relativeFilePath(fileName);
        this->slotSetValue(fileName);
    }
    editor->close(); 

}
